package cmgine.com.testqq;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;

import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMMessage;

import java.util.Iterator;
import java.util.List;

/**
 * Created by apple on 12/26/15.
 */
public class MyApplication extends Application {

    public static Context appContext;
    private  EMEventListener eventListener;
    @Override
    public void onCreate() {
        super.onCreate();

        appContext = this;
        EMChat.getInstance().init(appContext);
        EMChat.getInstance().setDebugMode(true);//在做打包混淆时，要关闭debug模式，避免消耗不必要的资源
        initListener();

    }
    private String getAppName(int pID) {
        String processName = null;
        ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List l = am.getRunningAppProcesses();
        Iterator i = l.iterator();
        PackageManager pm = this.getPackageManager();
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
            try {
                if (info.pid == pID) {
                    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
                    // Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
                    // info.processName +"  Label: "+c.toString());
                    // processName = c.toString();
                    processName = info.processName;
                    return processName;
                }
            } catch (Exception e) {
                // Log.d("Process", "Error>> :"+ e.toString());
            }
        }
        return processName;
    }

    private void initListener(){
        eventListener = new EMEventListener() {
            @Override
            public void onEvent(EMNotifierEvent event) {

                switch (event.getEvent()) {
                    case EventNewMessage:
                    {
                        EMMessage message = (EMMessage)event.getData();
                        Log.d("main",message.getBody().toString());
                        break;
                    }
                    case EventNewCMDMessage:
                    {
                        break;
                    }
                    // add other events in case you are interested in
                    default:
                        break;
                }

            }
        };
        EMChatManager.getInstance().registerEventListener(eventListener);
    }

}
